#pragma once
#include "TinyTCPServer.h"
#include "TinyTCPClient.h"
#include "Functor.h"
#include "TypelistMacros.h"
#include "TinyThread.h"
#include "TinyCC_Define.h"
#include "TinyMsg.h"

//using namespace TinyCC::TCP;
using namespace TinyCC::msg;
using namespace TinyCC;

class CTinyTCPIF
{
public:
	CTinyTCPIF(void);
	~CTinyTCPIF(void);
	void setTCPServer(TinyCC::TCP::CTinyTCPServer * tcpserver){m_TCPServer = tcpserver;}
	bool sendMsg(int socketID,CTinyMsg& msg);
	template <typename PointerToObj, typename PointerToMemFn >
	void setMsgProcFunc(const PointerToObj& pObj, PointerToMemFn pMemFun )
	{
		if( m_pMsgFunctor == NULL )
		{
			m_pMsgFunctor = new Loki::Functor< void,LOKI_TYPELIST_2(CTinyMsg&,CTinyMsg&) >( pObj, pMemFun );
		}
	}
	void setServerAddr(std::string serverIP, int serverPort)
	{
		m_ServerIP = serverIP;
		m_ServerPort = serverPort;
	}
private:
	TinyCC::TCP::CTinyTCPServer * m_TCPServer;
	Loki::Functor< void,LOKI_TYPELIST_2(CTinyMsg&,CTinyMsg&) >* m_pMsgFunctor;
public:
	void processTimer(std::string timerID);
	std::string m_ServerConnTimer;
	bool sendServerMsg(CTinyMsg& msg);
	bool checkServerConn(void);
	void serverMsgFunc(void* param);
	bool connectToServer(void);
// msg helper interface
public:
	void RaiseAlertEvt(string deviceID,string deviceType);
	void RaiseConnectedEvt(string deviceID,string deviceType);
public:
	TinyCC::TCP::CTinyTCPClient m_serverConn;
	std::string m_ServerIP;
	std::string m_LocalIP;
	int m_localPort;
	int m_ServerPort;
	HANDLE m_hRecHandle;
	CTinyThread *m_ServerMsgThread;
};
